<?php

class SitemapAction extends PublicAction
{

    public function html()
    {
        import('@.ORG.Tree');

        $cats = M('Category')->where('parentid=0 and lang=' . LANG_ID)->field('id,url,catname')->order('listorder asc')->select();

        foreach ($cats as $key => $val) {
            $data = M('Category')->where('parentid=' . $val['id'] . ' and lang=' . LANG_ID)->field('id,url,catname')->select();
            if ($data) {
                $cats[$key]['subcat'] = $data;
            }
        }

        $this->assign('sitemap', $cats);
        $this->display('index');
    }


    public function xml()
    {
        import("@.ORG.Cxml");
        $array = array();
        $array[0]['NodeName']['value'] = 'url';
        $array[0]['loc']['value'] = 'http://' . C('SITE_DOMAIN');
        $array[0]['lastmod']['value'] = date('Y-m-d', time());
        $array[0]['changefreq']['value'] = 'weekly';
        $array[0]['priority']['value'] = 1;

        $this->model = getCache('Model');
        foreach ((array)$this->model as $r) {

            if ($r['issearch']) {
                $num = 100;

                $data = M($r['tablename'])->field('id,title,url,create_time')->where("status=1")->order('id desc')->limit('0,' . $num)->select();

                foreach ($data as $key => $res) {
                    $arraya[$key]['NodeName']['value'] = 'url';
                    $arraya[$key]['loc']['value'] = 'http://' . C('SITE_DOMAIN') . $res['url'];
                    $arraya[$key]['lastmod']['value'] = date('Y-m-d', $res['create_time']);
                    $arraya[$key]['changefreq']['value'] = 'weekly';
                    $arraya[$key]['priority']['value'] = 0.7;
                }

                if (!empty($arraya)) {
                    $array = array_merge($array, $arraya);
                }
            }
        }

        $Cxml = new Cxml();
        $Cxml->root = 'urlset';
        $Cxml->root_attributes = array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9');

        $xmldata = $Cxml->Cxml($array);


        echo $xmldata;
    }
}